<template>
  <div class="markdown-content" v-html="renderedContent"></div>
</template>

<script>
import { computed } from 'vue'
import { marked } from 'marked'
import hljs from 'highlight.js'
import 'highlight.js/styles/github.css'

export default {
  name: 'MarkdownRenderer',
  props: {
    content: {
      type: String,
      default: ''
    }
  },
  setup(props) {
    // 配置marked选项
    marked.setOptions({
      breaks: true,
      gfm: true,
      headerIds: false,
      mangle: false
    })

    // 配置代码高亮
    marked.setOptions({
      highlight: function(code, lang) {
        if (lang && hljs.getLanguage(lang)) {
          try {
            return hljs.highlight(code, { language: lang }).value
          } catch (err) {
            console.error('代码高亮失败:', err)
          }
        }
        return hljs.highlightAuto(code).value
      }
    })

    const renderedContent = computed(() => {
      if (!props.content) return ''
      try {
        // 预处理markdown内容，修复标题格式
        let processedContent = props.content

        // 修复标题格式：确保#后面有空格
        // 匹配1-6个#，后面可能没有空格，然后是标题内容
        processedContent = processedContent.replace(/^(#{1,6})([^\s#])/gm, '$1 $2')

        // 调试输出
        // console.log('原始内容:', props.content)
        // console.log('预处理后:', processedContent)

        // 使用marked解析
        const html = marked.parse(processedContent)
        // console.log('解析后HTML:', html)

        return html
      } catch (error) {
        console.error('Markdown解析失败:', error)
        // 如果解析失败，手动处理markdown语法
        let processed = props.content

        // 预处理：修复标题格式
        processed = processed.replace(/^(#{1,6})([^\s#])/gm, '$1 $2')

        // 处理标题（包含有空格和无空格的情况）
        processed = processed.replace(/^######\s*(.*$)/gm, '<h6>$1</h6>')
        processed = processed.replace(/^#####\s*(.*$)/gm, '<h5>$1</h5>')
        processed = processed.replace(/^####\s*(.*$)/gm, '<h4>$1</h4>')
        processed = processed.replace(/^###\s*(.*$)/gm, '<h3>$1</h3>')
        processed = processed.replace(/^##\s*(.*$)/gm, '<h2>$1</h2>')
        processed = processed.replace(/^#\s*(.*$)/gm, '<h1>$1</h1>')

        // 处理粗体
        processed = processed.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')

        // 处理斜体
        processed = processed.replace(/\*(.*?)\*/g, '<em>$1</em>')

        // 处理代码块
        processed = processed.replace(/`(.*?)`/g, '<code>$1</code>')

        // 处理换行
        processed = processed.replace(/\n\n/g, '</p><p>')
        processed = processed.replace(/\n/g, '<br>')
        processed = '<p>' + processed + '</p>'

        // 清理空段落
        processed = processed.replace(/<p><\/p>/g, '')

        return processed
      }
    })

    return {
      renderedContent
    }
  }
}
</script>

<style scoped>
.markdown-content {
  line-height: 1.6;
  font-size: 14px;
  color: #333;
}

.markdown-content :deep(h1),
.markdown-content :deep(h2),
.markdown-content :deep(h3),
.markdown-content :deep(h4),
.markdown-content :deep(h5),
.markdown-content :deep(h6) {
  margin: 16px 0 8px 0;
  font-weight: 600;
}

.markdown-content :deep(h1) {
  font-size: 24px;
}

.markdown-content :deep(h2) {
  font-size: 20px;
}

.markdown-content :deep(h3) {
  font-size: 18px;
}

.markdown-content :deep(p) {
  margin: 8px 0;
}

.markdown-content :deep(ul),
.markdown-content :deep(ol) {
  margin: 8px 0;
  padding-left: 24px;
}

.markdown-content :deep(li) {
  margin: 4px 0;
}

.markdown-content :deep(blockquote) {
  border-left: 4px solid #ddd;
  padding-left: 16px;
  margin: 16px 0;
  color: #666;
}

.markdown-content :deep(code) {
  background: #f5f5f5;
  padding: 2px 4px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  font-size: 13px;
}

.markdown-content :deep(pre) {
  background: #f8f8f8;
  padding: 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 12px 0;
}

.markdown-content :deep(pre code) {
  background: none;
  padding: 0;
}

.markdown-content :deep(table) {
  border-collapse: collapse;
  width: 100%;
  margin: 12px 0;
}

.markdown-content :deep(th),
.markdown-content :deep(td) {
  border: 1px solid #ddd;
  padding: 8px 12px;
  text-align: left;
}

.markdown-content :deep(th) {
  background: #f5f5f5;
  font-weight: 600;
}

.markdown-content :deep(a) {
  color: #007bff;
  text-decoration: none;
}

.markdown-content :deep(a:hover) {
  text-decoration: underline;
}
</style>